package com.yunji.storage.pattern;

import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.utils.DecimalUtils;
import com.yunji.storage.domain.StockDO;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.storage.pattern
 * @Title: ProductInteract
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-09-12 17:41:09      V1.0        HuaAo       新建类
 */
@Component
public class ProductInteract extends AbstractStorage {

    @Override
    protected void addStorage(StockDO stock) {
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        // 生产入库是操作可用库存
        logs.info("【生产单库存处理】新增库存:{}", actualQuantity);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setUsableStock(actualQuantity);
        update.setAllStock(actualQuantity);

        Assert.isTrue(stockService.updateStock(update), "入库失败");
    }

    @Override
    protected void subtractStock(StockDO stock) {
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        // 生产出库操作可用库存
        logs.info("【生产单库存处理】扣减库存:{}", actualQuantity);

        BigDecimal usableStock = stock.getUsableStock();
        logs.info("【生产单库存处理】扣减库存前的可用库存:{}", usableStock);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setUsableStock(actualQuantity.negate());
        update.setAllStock(actualQuantity.negate());

        Assert.isTrue(!DecimalUtils.more(actualQuantity, usableStock), "可用库存不足");
        Assert.isTrue(stockService.updateStock(update), "出库失败");

    }

    @Override
    protected void backStock(StockDO stock) {
        logs.info("【生产单库存处理】返还库存:{}", stock);
    }

    @Override
    protected void createBackStock(StockDO stock) {
        logs.info("【生产库存处理】新增返还库存:{}", stock);
    }

    @Override
    public void updateTicket(Long businessId) {
        logs.info("【跟新生产单】生产单ID:{}", businessId);
    }

    @Override
    public void checkTicket(String businessCode) {
        Assert.isTrue(Boolean.FALSE, ErrorEnum.BUSINESS_812016.getText());
    }
}